﻿namespace("Sieena.Common");

(function($, undefined) {

Sieena.Common.GetDayText = function(day){
    switch(day)
    {
        case 1: return "First";
        case 2: return "Second";
        case 3: return "Third";
        case 4: return "Fourth";
        case 5: return "Fifth";
        case 6: return "Sixth";
        case 7: return "Seventh";
        case 8: return "Eigth";
        case 9: return "Ninth";
        case 10: return "Tenth";
        case 11: return "Eleventh";
        case 12: return "Twelfth";
        case 13: return "Thirteenth";
        case 14: return "Fourteenth";
        case 15: return "Fifteenth";
        case 16: return "Sixteenth";
        case 17: return "Seventeenth";
        case 18: return "Eighteenth";
        case 19: return "Nineteenth";
        case 20: return "Twentieth";
        case 21: return "Twenty-First";
        case 22: return "Twenty-Second";
        case 23: return "Twenty-Third";
        case 24: return "Twenty-Fourth";
        case 25: return "Twenty-Fifth";
        case 26: return "Twenty-Sixth";
        case 27: return "Twenty-Seventh";
        case 28: return "Twenty-Eighth";
        case 29: return "Twenty-Ninth";
        case 30: return "Thirtieth";
        case 31: return "Thirty-First";
        default: return "";
    }
};

Sieena.Common.GetMonthName = function(month)
{
    switch(month)
    {
        case 1: return "January";
        case 2: return "February";
        case 3: return "March";
        case 4: return "April";
        case 5: return "May";
        case 6: return "June";
        case 7: return "July";
        case 8: return "August";
        case 9: return "September";
        case 10: return "October";
        case 11: return "November";
        case 12: return "December";
        default: return "";
    }
}

Sieena.Common.AjaxErrorHandler = function (req, status, error) {
    if (req.status == 403) {
        alert('You are not authorized to perform this action.');
    }
    Sieena.Common.DisplayNotification('error', 'Error in request.', "There was a problem with the system.");
    log('Error in request.', req, status, error);
};

Sieena.Common.HandleJsonResult = function (form, result) {
    var valid = false;
    switch (result.StatusCode) {
        case 0: //Success
            Sieena.Common.DisplaySavedMessage(form);
            valid = true;
            break;
        case 1: //SystemError
            Sieena.Common.DisplayNotification('error', 'Error on Save', "An error occurred while saving the information.");
            break;
        case 2: //ValidationError
            Sieena.Common.ValidationWarning(form, result.Validation);
            break;
    }
    return valid;
};

Sieena.Common.HandleSuccessfulForm = function (responseText, statusText, xhr, form) {
    var result = JSON.parse(responseText);
    Sieena.Common.HandleJsonResult(form, result);
};

Sieena.Common.DisplayNotification = function (type, title, msg, time) {
    var icon, isSticky = typeof time == 'undefined' ? false : !time,
        time = time || 5000;

    switch (type) {
        case 'success':
            icon = Sieena.Resources.Icons.Ok;
            break;
        case 'info':
            icon = Sieena.Resources.Icons.Info;
            break;
        case 'alert':
            icon = Sieena.Resources.Icons.Alert;
            break;
        case 'error':
            icon = Sieena.Resources.Icons.Error;
            isSticky = true;
            break;
    }

    return $.gritter.add({
        title: title,
        text: msg,
        sticky: isSticky,
        time: time,
        image: icon
    });
};

Sieena.Common.RemoveNotification = function (notificationId) {
    if (notificationId) {
        $.gritter.remove(notificationId, {
            fade: true,
            speed: 'fast'
        });
    }
};

Sieena.Common.DisplaySavedMessage = function (form, message) {
    message = message || "Your information was successfully saved!";
    Sieena.Common.ClearValidationMessages(form);
    Sieena.Common.DisplayNotification('success', 'Form Saved', message);
};

Sieena.Common.ValidationWarning = function (form, validationResult) {
    var message = "Please review validation messages.";
    Sieena.Common.DisplayNotification('alert', 'Some information is not valid', message);
    var ul = form.find(".validation-summary-errors, .validation-summary-valid").addClass("validation-summary-errors").removeClass("validation-summary-valid").find("ul");
    $.each(validationResult.Errors || [], function () {
        $("<li>").text(this.ErrorMessage).appendTo(ul);
    });
};

Sieena.Common.ClearValidationMessages = function (form) {
    form.find(".validation-summary-errors, .validation-summary-valid").addClass("validation-summary-valid").removeClass("validation-summary-errors").find("ul").empty();
};

})(jQuery);
